package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.webUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.tools.DocumentationTool;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 14587
 * @date 2020/10/24 15:03
 */
public class CartServlet extends BaseServlet{
    private BookService bookService=new BookServiceImpl();
    /**
     * @author VegetDog
     * @Description 添加商品
     * @date 2020-10-24 15:12
     * @param req
 * @param resp
     * @return void
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的id
        int id = webUtils.parseInt(req.getParameter("id"), 0);
        //调用queryBookByid方法获取对应的商品信息，
        Book book = bookService.queryBookById(id);
        //把图书信息转换成CartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //调用cart.additem方法添加商品
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if (cart==null){
            cart=new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //重定向回原来商品所在的商品列表页面
     /*   System.out.println(req.getHeader("Referer"));*/
        req.getSession().setAttribute("lastName",cartItem.getName());
        resp.sendRedirect(req.getHeader("Referer"));
    }
/**
 * @author VegetDog
 * @Description 删除商品项
 * @date 2020-10-24 15:59
 * @param req
 * @param resp
 * @return void
 */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的id
        int id = webUtils.parseInt(req.getParameter("id"), 0);
        //获取购物车对象
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if (cart!=null){
            cart.deleteItem(id);
            //重定向回原来的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
/**
 * @author VegetDog
 * @Description 清空购物车
 * @date 2020-10-24 16:12
 * @param req
 * @param resp
 * @return void
 */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //不为空的话就清空
        if (cart!=null){
            cart.clear();
        }
        //重定向回原来的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }
/**
 * @author VegetDog
 * @Description 修改商品数量
 * @date 2020-10-24 16:33
 * @param req
 * @param resp
 * @return void
 */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取请求id和数量
        int id = webUtils.parseInt(req.getParameter("id"), 0);
        int count = webUtils.parseInt(req.getParameter("count"), 1);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!=null){
            //修改商品数量
            cart.updateCount(id,count);
            //重定向回原来的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的id
        int id = webUtils.parseInt(req.getParameter("id"), 0);
        //调用queryBookByid方法获取对应的商品信息，
        Book book = bookService.queryBookById(id);
        //把图书信息转换成CartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //调用cart.additem方法添加商品
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if (cart==null){
            cart=new Cart();
        }
        cart.addItem(cartItem);
        req.getSession().setAttribute("cart",cart);
        //使用ajax请求回传数据
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());
        req.getSession().setAttribute("lastName",cartItem.getName());

        Gson gson=new Gson();
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
       // req.getRequestDispatcher("/index.jsp").forward(req,resp);
        //resp.sendRedirect(req.getContextPath()+"/index.jsp");
    }
}
